package org.swust.sysmonitorapp.controller;

import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.swust.sysmonitorapp.entity.R;
import org.swust.sysmonitorapp.entity.SysDepartment;
import org.swust.sysmonitorapp.service.ISysDepartmentService;


/**
 * 部门表 前端控制器
 *
 * @since 2024-12-25
 */
@RestController
@RequestMapping("/dept")
public class SysDepartmentController {
    @Autowired
    private ISysDepartmentService departmentService;

    /**
     * 通过id查询部门信息
     *
     * @param id 部门id
     * @return 部门信息
     */
    @GetMapping("/{id}")
    public R<SysDepartment> getById(@PathVariable(name = "id") Long id) {
        return R.ok(departmentService.getById(id));
    }

    /**
     * 添加部门
     *
     * @param department 部门信息
     * @return success/false
     */
    @PostMapping
    public R<Boolean> addDepartment(@RequestBody SysDepartment department) {
        return R.ok(departmentService.save(department));
    }

    /**
     * 更新部门信息
     *
     * @param department 部门信息
     * @return R
     */
    @PutMapping
    public R<Boolean> updateDepartment(@Valid @RequestBody SysDepartment department) {
        return R.ok(departmentService.updateById(department));
    }

    /**
     * 删除部门
     *
     * @param id 部门id
     * @return R
     */
    @DeleteMapping("/{id}")
    public R<Boolean> deleteDepartment(@PathVariable Long id) {
        return R.ok(departmentService.removeById(id));
    }
}
